// Copyright 2009 Google Inc. All Rights Reserved.

/**
 * @fileoverview Code for handling functionality specific to home view.
 * @author gadgetfactory@google.com (gadgetfactory)
 */

/**
 * Index of selected item in current category.
 * @type {number}
 */
var selectedItem = 0;

//TODO: remove left/right images...
/**
 * Left navigation arrow.
 * @const
 */
var LEFT_ACTIVE_IMAGE = 'http://foodziegadget.googlecode.com/svn/trunk/' +
    'foodzie_content/foodzie_left.gif';

/**
 * Left navigation disabled arrow.
 * @const
 */
var LEFT_INACTIVE_IMAGE = 'http://foodziegadget.googlecode.com/svn/trunk/' +
    'foodzie_content/foodzie_left_disabled.png';

/**
 * Right navigation arrow.
 * @const
 */
var RIGHT_ACTIVE_IMAGE = 'http://foodziegadget.googlecode.com/svn/trunk/' +
    'foodzie_content/foodzie_right.gif';

/**
 * Right navigation disabled arrow.
 * @const
 */
var RIGHT_INACTIVE_IMAGE = 'http://foodziegadget.googlecode.com/svn/trunk/' +
    'foodzie_content/foodzie_right_disabled.png';

/**
 * Shows the content.
 * @param {number} num number to be added in current item index.
 */
function showContent(num) {
  if ((num < 0 && selectedItem == 0) ||
      (num > 0 && selectedItem == content.length - 1)) {
    return;
  }
  selectedItem = selectedItem + num;
  if (!selectedItem) {
    _gel('foodzie-prev-link').style.display = 'none';
  } else {
    _gel('foodzie-prev-link').style.display = 'block';
  }
  if (selectedItem == content.length - 1) {
    _gel('foodzie-next-link').style.display = 'none';
  } else {
    _gel('foodzie-next-link').style.display = 'block';
  }
  _gel('foodzie-item-div').style.display = 'block';
  _gel('foodzie-item-div').style.width = content[selectedItem].imgwidth;
  _gel('foodzie-item-div').style.height = content[selectedItem].imgheight;
  _gel('foodzie-item-image').src = content[selectedItem].img;
  _gel('foodzie-item-price').innerHTML = content[selectedItem].price;
  _gel('foodzie-item-link').href = content[selectedItem].link;
  var titleElement = _gel('foodzie-item-title');
  if (titleElement) {
    titleElement.innerHTML = content[selectedItem].title;
    titleElement.href = content[selectedItem].link;
  }
  _IG_AdjustIFrameHeight();
}

/**
 * Creates body for the gadget.
 */
function createBody() {
  if (!content.length) {
    showMessage(prefs.getMsg('no_item_found'));
    _gel('foodzie-prev-link').style.display = 'none';
    _gel('foodzie-next-link').style.display = 'none';
    return;
  }
  _gel('foodzie-content').style.display = 'block';
  selectedItem = 0;
  showContent(0);
}

/**
 * Switch to canvas view.
 */
function toCanvas() {
  if (_args()['synd'] == 'open') {
    var index = prefs.getInt('category');
    window.top.location = categoryObj[index].link;
    return;
  }
  gadgets.views.requestNavigateTo(new gadgets.views.View('canvas'), {});
}

/**
 * Executes when choose any category.
 */
function changeCategory() {
  var element = _gel('foodzie-category-select');
  var index = element ? element.value : 0;
  prefs.set('category', index);
  init();
}

// TODO: replace with better function
/* cross-browser window size guesser */
var winw = 0, winh = 0;
if (typeof (window.innerWidth) == 'number') {
  //Non-IE
  winw = window.innerWidth;
  winh = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientWidth ||
    document.documentElement.clientHeight)) {
  //IE 6+ in 'standards compliant mode'
  winw = document.documentElement.clientWidth;
  winh = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth ||
    document.body.clientHeight)) {
  //IE 4 compatible
  winw = document.body.clientWidth;
  winh = document.body.clientHeight;
}
// for cache-sharing reasons, round to the nearest 10 pixels
var rounded_h = 20 * parseInt(winh / 20);
var rounded_w = 20 * parseInt(winw / 20);
// leave room for UI elements and padding
rounded_h -= 80;
rounded_w -= 25;
/**
 * Initializes global variables and user prefs and proceed further execution.
 */
function init() {
  var index = prefs.getInt('category');
  var element = _gel('foodzie-category-select');
  if (element) {
    element.value = index;
  }
  if (index != -1 && categoryObj[index]) {
    var url = FEED_URL + '';
    url = url.replace('CID', categoryObj[index].id + '');
    url = url.replace('LIMIT', '10');
    fetchContent(url, rounded_w, rounded_h);
  }
}

// Entry point.
_IG_RegisterOnloadHandler(init);
